#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

void begin();
int main(){
	begin();	
	return 0;
}

void begin(){
	printf("begin\n");
	pid_t A=fork();
	if(A==-1){
		perror("A");
		exit(1);
	}
	if(A==0){
		printf("A fork:\n");
		printf("A %d\n",getpid());
		sleep(2);
		printf("A sleep YES\n");
		exit(2);
	}else{
		pid_t B=fork();
		if(B==-1){
		perror("B ");
		exit(1);
		}
		if(B==0){
			printf("B fork:\n");
			printf("B %d\n",getpid());
			sleep(1);
			printf("B sleep YES\n");
			exit(6);
		}else{
			pid_t status=0;
			pid_t cpid=0;
			printf("====waitpid====\n");
			while((cpid=waitpid(-1,&status,WNOHANG))==0);
			printf("cpid=%d WEXITSTATUS=%d\n",cpid,WEXITSTATUS(status));
			while((cpid=waitpid(-1,&status,WNOHANG))==0);
			printf("cpid=%d WEXITSTATUS=%d\n",cpid,WEXITSTATUS(status));
		}
	}
}
